<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class HomesController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Homes';

/**
 * Default helper
 *
 * @var array
 */
	
	public $helpers = array('Html', 'Session','Common');

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Error','Image','ImageCategory','News','StaticPage');
	
	public $components=array('Email');

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 */
	public function index() {
		//$this->Error->find('all');
		$this->layout='default';
		$this->set('css','home');
		$this->set('title_for_layout','Câu lạc bộ thiện nguyện SCY');
		//t($this->News->find('all',array('order'=>'rand()','limit'=>3)));
		$this->Image->recursive=0;
		$imgs=$this->Image->find('all',array('conditions'=>array('ImageCategory.published'=>1,'Image.published'=>1),'fields'=>array('id','image_category_id','image','title'),'order'=>'rand()','limit'=>10));
		foreach($imgs as $k=>$v){
			$cat=$this->ImageCategory->findById($v['Image']['image_category_id']);
			$imgs[$k]['Image']['cattitle']=$cat['ImageCategory']['title'];
		}
		$this->set('images',$imgs);
		$this->set('news',$this->News->find('all',array('conditions'=>array('News.published'=>1),'order'=>'News.home desc, News.id desc','limit'=>3)));
		//$this->redirect("homes/home");
	}
	public function about(){
		$this->layout='default';
		$this->set('css','about');
		$this->set('title_for_layout','Đôi nét về câu lạc bộ thiện nguyện SCY');
		$rs='';
		if(isset($this->request->data) && !empty($this->request->data['name']) && !empty($this->request->data['email']) && !empty($this->request->data['content'])){
			//if($this->data['Home']['sec']==$this->Session->read('Captcha')){
			if(!$this->Session->check('lastSendMail') ||  (time()- $this->Session->read('lastSendMail'))>$time){
				//Bat dau xu ly viec goi email
				
				$email_content = "Thông tin người gửi:<br /><br />";			
				$email_content .= "Người gửi: " .$this->request->data['name']."<br />";
				$email_content .= "Email: " .$this->request->data['email']."<br />";
				$email_content .= "Số điện thoại: " .$this->request->data['phone']."<br />";
				$email_content .= "Nội dung: " .$this->request->data['content']."<br />";
				
				
				$email = new CakeEmail();
				$email->from(array($this->request->data['email'] => $this->request->data['name']));
				$email->subject('Email phản hồi của người dùng.');				
				$email->emailFormat('html');
				$emails= explode(',',$this->webConfig['emailContact']);
				if(count($emails)>1){
					foreach($emails as $k=>$v){
						$email->to($v);
						$result = $email->send($email_content);	
					}				
				}
				else{
					$email->to($this->webConfig['emailContact']);
					$result = $email->send($email_content);
				}				
				
				if($result){
					$rs= "<script>alert('".__('Cám ơn đã gửi mail liên hệ!',true)."')</script>";
					$this->data=null;
				}
				else{
					$rs="<script>alert('".__('Send email error!',true)."')</script>";
				}
				$this->Session->write('lastSendMail',time());
			}else{
				$rs="<script>alert('".__('Bạn đã gửi mail liên hệ rồi, hãy đợi '.$time.' giây rồi gửi tiếp.',true)."')</script>";
			}
		}	
		
		$data=null;
		$contact=null;
		$this->StaticPage->locale=$this->Session->read('siteLang');
		$data=$this->StaticPage->findByKeycode('about');
		$contact=$this->StaticPage->findByKeycode('contact');	

		$this->set('data',$data['StaticPage']['content']);
		$this->set('contact',$contact['StaticPage']['content']);	
		$this->set('result',$rs);
		$this->render('about');	
		
	}
	
	public function admin_index(){
		$this->layout='admin_default';
	}
	public function admin_home(){
		$this->render('admin_home');
	}
}
